﻿/*-----------------------------------------------
// File: DropAngerBall.cs
// Description: 
// Author: Shaobing	492057342@qq.com
-----------------------------------------------*/
using UnityEngine;
using System.Collections;

namespace Battle.View
{
    public class DropAngerBall : DropBall 
    {
        protected Vector3 initPosition;
        protected Vector3 dropDir;
        protected Vector3 initSpeed;
        protected Vector3 acceleration;
        public override float GetDropDownTime()
        {
            return dropDownTime;
        }

        public override float GetDropDownWaitTime()
        {
            return dropDownWaitTime;
        }

        public float dropDownTime = 0.7f;
        public float dropDownWaitTime = 1.5f;


        /// <summary>
        /// 谁砍出来的 
        /// </summary>
        public BattleCharacter causeBC;

        public enum DropPhase
        {
            Circle_1,
            Circle_2,
            Circle_3,
            Ground,
            FlyBack
        }

        protected DropPhase currentPhase;

        protected override void Drop0()
        {
            // InitPosition
            if (!isHForce)
            {
                var h = Random.Range(dropBC.height / 2, dropBC.height);
                this.initPosition = dropBC.transform.localPosition + new Vector3(0, h, 0);
            }
            // DropDir
            if (!isDirForce)
            {
                dropDir = new Vector3(Random.Range(-100, 100), 0, Random.Range(-100, 100));
                dropDir = dropDir.normalized;
            }
            //var v0 = (g * GetDropDownTime() - Mathf.Sqrt(2 * g * h)) / 2;
            if (!isSpeedForce)
            {
                var v0 = 3;
                initSpeed = dropDir * Random.Range(1f, 1.5f) + new Vector3(0, v0, 0);
            }
            Debug.Log("pos:" + initPosition + " dir:" + dropDir + " speed:" + initSpeed);
            acceleration = new Vector3(0, -g, 0);

            currentPhase = DropPhase.Circle_1;
            curInitPosition = initPosition;
            curInitSpeed = initSpeed;
        }

        bool isDirForce = false;
        bool isSpeedForce = false;
        bool isHForce = false;

        public virtual void Drop(BattleCharacter bc, BattleCharacter targetBC, float hPercent, Vector3 dir, Vector3 v)
        {
            if (bc != null)
            {
                this.dropBC = bc;
                this.causeBC = targetBC;
                transform.SetParent(BattleViewCtrl.GetInstance().transform);
                transform.localPosition = initPosition;
                transform.LookAt(transform.localPosition + dropDir * 100);
                isDirForce = true;
                isSpeedForce = true;
                isHForce = true;
                initPosition = dropBC.transform.localPosition + new Vector3(0, dropBC.height / 2 * (1 + hPercent), 0);
                dropDir = dir.normalized;
                initSpeed = dropDir + v;
                Drop0();
                isRun = true;
                t = 0;
            }
            else
            {
                BattleFactory.UnSpawn(gameObject);
            }
        }

        public virtual void Drop(BattleCharacter source, BattleCharacter target)
        {
            if (source != null && target != null)
            {
                dropBC = source;
                causeBC = target;

                transform.SetParent(BattleViewCtrl.GetInstance().transform);
                transform.localPosition = initPosition;
                transform.LookAt(transform.localPosition + dropDir * 100);
                isDirForce = false;
                isSpeedForce = false;
                isHForce = false;
                Drop0();
                isRun = true;
                t = 0;
            }
            else
            {
                BattleFactory.UnSpawn(gameObject);
            }
        }

        public void Drop(BattleCharacter bc) {
            if (bc != null)
            {
                this.dropBC = bc;
                transform.SetParent(BattleViewCtrl.GetInstance().transform);
                transform.localPosition = initPosition;
                transform.LookAt(transform.localPosition + dropDir * 100);
                isDirForce = false;
                isSpeedForce = false;
                isHForce = false;
                Drop0();
                isRun = true;
                t = 0;
            }
            else
            {
                BattleFactory.UnSpawn(gameObject);
            }
        }


        protected float rotateXSpeed = 720f;
        protected float rotateXSpeedGround = 540f;
        protected float t = 0;                // DropDown 当前时间

        protected Vector3 curInitPosition;
        protected Vector3 curInitSpeed;

        protected override void DropTick()
        {
            if (isRun)
            {
                if (currentPhase == DropPhase.Circle_1)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (newPos.y <= 0.2)
                    {
                        currentPhase = DropPhase.Circle_2;
                        curInitPosition = transform.localPosition;
                        var curSpeed = curInitSpeed + acceleration * t;
                        curSpeed.y = -curSpeed.y;
                        curInitSpeed = curSpeed * 2 / 3;
                        t = 0f;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Circle_2)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (t >= 2 * curInitSpeed.y / g)
                    {
                        currentPhase = DropPhase.Circle_3;
                        curInitPosition = transform.localPosition;
                        var curSpeed = curInitSpeed + acceleration * t;
                        curSpeed.y = -curSpeed.y;
                        curInitSpeed = curSpeed * 2 / 3;
                        t = 0f;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Circle_3)
                {
                    t += Time.deltaTime;
                    var newPos = curInitPosition + curInitSpeed * t + 0.5f * acceleration * t * t;
                    if (t >= 2 * curInitSpeed.y / g)
                    {
                        currentPhase = DropPhase.Ground;
                        t = 0f;
                        curInitPosition = transform.localPosition;
                        curInitSpeed = Vector3.zero;
                        groundTime = 0f;
                        isStand = false;
                        return;
                    }
                    transform.localPosition = newPos;
                    transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                }
                else if (currentPhase == DropPhase.Ground)
                {

                    if (isStand)
                    {
                        groundTime += Time.deltaTime;
                        if (groundTime >= 1.5f)
                        {
                            //isRun = false;
                            //Dispear();
                            if (causeBC != null)
                            {
                                flyBeginPos = transform.localPosition;
                                flyTime = 0f;
                                currentPhase = DropPhase.FlyBack;
                            }
                            else
                            {
                                isRun = false;
                                Dispear();
                            }
                            return;
                        }
                    }
                    else
                    {
                        transform.Rotate(rotateXSpeed * Time.deltaTime, 0, 0);
                        if ((Mathf.Abs(transform.localEulerAngles.x - 90f) < Time.deltaTime * rotateXSpeed))
                        {
                            isStand = true;
                            transform.localEulerAngles = new Vector3(90f, transform.localEulerAngles.y, transform.localEulerAngles.z);
                            return;
                        }
                        if (Mathf.Abs(transform.localEulerAngles.x + 90f) <= Time.deltaTime * rotateXSpeed)
                        {
                            isStand = true;
                            transform.localEulerAngles = new Vector3(-90f, transform.localEulerAngles.y, transform.localEulerAngles.z);
                            return;
                        }
                    }
                }
                else if (currentPhase == DropPhase.FlyBack)
                {
                    flyTime += Time.deltaTime;

                    Vector3 targetPos = causeBC.transform.localPosition + new Vector3(0, causeBC.height / 2, 0);
                    float leftDis = Vector3.Distance(transform.localPosition, targetPos);
                    if (leftDis <= Time.deltaTime * leftSpeed)
                    {
                        transform.localPosition = targetPos;
                        isRun = false;
                        Dispear();
                        return;
                    }
                    var leftDir = targetPos - transform.localPosition;
                    leftDir = leftDir.normalized;

                    //float leftSpeed = leftDis / leftTime; 
                    var newPos = transform.localPosition + leftSpeed * leftDir * Time.deltaTime;
                    transform.localPosition = newPos;

                }
            }
        }

        protected float groundTime = 0f;
        protected bool isStand = false;


        protected float leftSpeed = 10f;

        protected Vector3 flyBeginPos;
        protected float flyTime = 0f;
        protected float flySpeed = 0f;
        protected float flyAcc = 10f;


        protected override void Dispear() {
            var obj = BattleFactory.SpawnDropBallDispear();
            obj.transform.SetParent(causeBC.transform);
            obj.transform.localPosition = new Vector3(0, causeBC.height / 2f, 0);
            obj.transform.localScale = Vector3.one;
            obj.transform.localEulerAngles = Vector3.zero;
            BattleFactory.UnSpawn(gameObject);
            BattleFactory.UnSpawn(obj, 1.8f);
        }
    }
}